package com.tea.room.timer;

import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.tea.room.model.WeChatToken;
import com.tea.room.model.tea.TeaOrder;
import com.tea.room.service.door.DoorService;
import com.tea.room.service.tea.TeaOrderService;
import com.tea.room.service.wechat.WeChatService;
import com.tea.room.service.wechat.impl.WeChatUtil;
import com.tea.room.util.ConfigUtil;
import com.tea.room.util.DateUtils;

@Component
public class TimerTask {
    
    private static Logger logger = LoggerFactory.getLogger(TimerTask.class);

    private static Properties wechat = ConfigUtil.getProperties("wechat.properties");
    
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private TeaOrderService teaOrderService;
    @Autowired
    private DoorService doorService;

    /**
     * 获取微信token
     */
//    @Scheduled(fixedRate = 2 * 60 * 60 * 1000)
    public void getAccess_token(){
        logger.info("wechat access_token 定时任务开始" + new Date().getTime());
        String str = WeChatUtil.getAccess_token(wechat.getProperty("ACCESS_TOKEN_URL"), wechat.getProperty("APPID"), wechat.getProperty("APPSECRET"));
        JSONObject object = JSONObject.parseObject(str);
        String access_token = String.valueOf(object.get("access_token"));
        String expires_in = String.valueOf(object.get("expires_in"));
        WeChatToken token = new WeChatToken();
        token.setAccess_token(access_token);
        token.setExpires_in(expires_in);
        token.setRiseTime(new Date());
        token.setFailureTime(DateUtils.getAfterHours(2));
        token.setToken_type("wechat");
        token.setData_flag("1");
        weChatService.insertAccessToken(token);
        logger.info("wechat access_token 定时任务结束" + new Date().getTime());
    }
    
//    @Scheduled(fixedRate = 2 * 60 * 60 * 1000)
    public void getDoorAccess_token(){
        logger.info("door access_token 定时任务开始" + new Date().getTime());
        String login = doorService.getAccessToken();
        JSONObject object = JSONObject.parseObject(login);
        String access_token = String.valueOf(object.get("token"));
        String expires_in = String.valueOf(object.get("expiresIn"));
        WeChatToken token = new WeChatToken();
        token.setAccess_token(access_token);
        token.setExpires_in(expires_in);
        token.setRiseTime(new Date());
        token.setFailureTime(DateUtils.getAfterHours(2));
        token.setToken_type("door");
        token.setData_flag("1");
        doorService.insertAccessToken(token);
        logger.info("door access_token 定时任务结束" + new Date().getTime());
    }
    
//    /**
//     * 房间开始提醒
//     */
//    @Scheduled(cron=TimerConfig.REMINDERTASK)
//    public void startTask(){
//        logger.info("服务开始提醒 定时任务开始" + new Date().getTime());
//        List<TeaOrder> list = teaOrderService.findStartOrder();
//        for (TeaOrder teaOrder : list) {
//            weChatService.startNotice(teaOrder);
//        }
//        logger.info("服务开始提醒 定时任务结束" + new Date().getTime());
//    }
//    
    /**
     * 房间到期提醒打扫
     */
    @Scheduled(cron=TimerConfig.REMINDERTASK)
    public void reminderTask(){
        logger.info("服务到期提醒 定时任务开始" + new Date().getTime());
        List<TeaOrder> list = teaOrderService.findOrder();
        for (TeaOrder teaOrder : list) {
            weChatService.messageNotice(teaOrder);
        }
        logger.info("服务到期提醒 定时任务结束" + new Date().getTime());
    }
    
    /**
     * 无效订单删除
     */
    @Scheduled(cron=TimerConfig.DELETEORDER)
    public void deleteOrder(){
        logger.info("删除无效订单  定时任务开始" + new Date().getTime());
        teaOrderService.deleteOrder(new TeaOrder());
        logger.info("删除无效订单  定时任务结束" + new Date().getTime());
        logger.info("删除无效token  定时任务开始" + new Date().getTime());
        doorService.removeAccessToken();
        logger.info("删除无效token  定时任务结束" + new Date().getTime());
    }
    
    
}
